
#include "global_avg_pool_tiling.h"
#include "register/op_def_registry.h"

namespace optiling {

constexpr int32_t CORE_NUM = 1;
constexpr int32_t UB_SIZE = 196608;

constexpr int32_t X_INDEX = 0;
constexpr int32_t BYTE_BLOCK = 32;

constexpr int32_t FP16_BUF_COUNT = (1) * 1 + 2 * 2;
constexpr int32_t FP32_BUF_COUNT = (1) * 2 + 1;

static ge::graphStatus TilingFunc(gert::TilingContext* context) {
    GlobalAvgPoolTilingData tiling;
    uint32_t totalCoreNum = CORE_NUM;
    uint64_t ubSizePlatForm = UB_SIZE;

    const gert::Shape xShape = context->GetInputShape(X_INDEX)->GetStorageShape();
    const ge::DataType dataType = context->GetInputDesc(X_INDEX)->GetDataType();
    int32_t dTypeSize = ge::GetSizeByDataType(dataType);
    int32_t perBlockCount = BYTE_BLOCK / dTypeSize;
    int64_t totalDataCount = xShape.GetShapeSize();

    int64_t bufferCount = FP32_BUF_COUNT;
    context->SetTilingKey(101);
    if (dataType != ge::DT_FLOAT) {
        context->SetTilingKey(201);
        bufferCount = FP16_BUF_COUNT;
    }
    uint64_t ubMaxProcCount = ubSizePlatForm / bufferCount / BYTE_BLOCK * perBlockCount;
    tiling.set_ubMaxProcCount(ubMaxProcCount);
    tiling.set_totalDataCount(totalDataCount);
    tiling.set_ncSize(xShape.GetDim(0) * xShape.GetDim(1));

    int64_t hwSize = 1;
    for (size_t i = 2; i < xShape.GetDimNum(); i++) {
        hwSize *= xShape.GetDim(i);
    }

    tiling.set_hwSize(hwSize);
    tiling.set_loopTime(hwSize / ubMaxProcCount);
    tiling.set_tailCount(hwSize % ubMaxProcCount);

    context->SetBlockDim(totalCoreNum);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    printf(
        "==zf==Tiling Data, ubMaxProcCount:%lu, totalDataCount:%lu, ncSize:%lu, hwSize:%lu, loopTime:%lu, "
        "tailCount:%lu\n",
        tiling.get_ubMaxProcCount(), tiling.get_totalDataCount(), tiling.get_ncSize(), tiling.get_hwSize(),
        tiling.get_loopTime(), tiling.get_tailCount());

    // 以下代码是ascendebug必须的
    size_t* workspaces = context->GetWorkspaceSizes(1);
    workspaces[0] = 16 * 1024 * 1024;
    return ge::GRAPH_SUCCESS;
}
}  // namespace optiling

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context) {
    return GRAPH_SUCCESS;
}
}  // namespace ge

namespace ops {
class GlobalAvgPool : public OpDef {
public:
    explicit GlobalAvgPool(const char* name) : OpDef(name) {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore().SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend910b");
    }
};

OP_ADD(GlobalAvgPool);
}  // namespace ops
